package com.jiayi.weiphoto.page;

import com.jiayi.weiphoto.util.MyLog;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class FixedWidthImageView extends ImageView{
	
	public FixedWidthImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public FixedWidthImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public FixedWidthImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		MyLog.e("");
	}
	
	@Override
	public void setImageBitmap(Bitmap bitmap) {
		// TODO Auto-generated method stub
		float scale = 1;
		float mWidth = getWidth();
		if (bitmap != null) {
			scale = (float)mWidth / bitmap.getWidth();
			if(getLayoutParams() instanceof LinearLayout.LayoutParams){
				setLayoutParams(new LinearLayout.LayoutParams((int)(mWidth), (int)(bitmap.getHeight()*scale)));
			}else if(getLayoutParams() instanceof RelativeLayout.LayoutParams){
				setLayoutParams(new RelativeLayout.LayoutParams((int)(mWidth), (int)(bitmap.getHeight()*scale)));
			}
			setScaleType(ScaleType.FIT_XY);
			super.setImageBitmap(bitmap);
		}
	}
	
	@Override
	protected void onLayout(boolean changed, int left, int top, int right,
			int bottom) {
		// TODO Auto-generated method stub
		super.onLayout(changed, left, top, right, bottom);
//		MyLog.e("left is "+left+"  top is "+top+"right is "+right+" bottom is "+bottom);
	}

}
